package com.easymin.weixin.controller.v4;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxApplicationManager;

@RestController("SystemConfRes")
@RequestMapping("api/system")
public class SystemConfRes {
  
  @Autowired
  private WxApplicationManager wxApplicationManager;

  /**
   * 根据服务器地址查询系统key.
   */
  @RequestMapping(value = {"findByAddress"}, method = RequestMethod.GET)
  public ResultTransport findOne(
      @RequestParam("address") String address) {
    ResultTransport resultTransport = new ResultTransport();
    if (StringUtils.isBlank(address)) {
      resultTransport.setCode(-1);
      resultTransport.setData(null);
      resultTransport.setMessage("参数错误");
    } else {
      if (!address.startsWith("http://") && !address.startsWith("https://")) {
        address = "http://"+address;
      }
      WxApplication wxApplication = wxApplicationManager.findByAddress(address);
      
      if (null != wxApplication) {
        resultTransport.setCode(0);
        resultTransport.setData(wxApplication.getWxkey());
      } else {
        resultTransport.setCode(-1);
        resultTransport.setData(null);
        resultTransport.setMessage("不存在");
      }
    }
    return resultTransport ;
  }
}
